package com.smart.fourteen.hibernate.template.dao;

import com.smart.fourteen.hibernate.model.Topic;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
@Transactional
public class TopicDao extends BaseDao {
    public void addTopic(Topic topic){
        getHibernateTemplate().save(topic);
    }

    public void updateTopic(Topic topic){
        getHibernateTemplate().update(topic);
    }

    public Topic getTopic(int id){
        return getHibernateTemplate().get(Topic.class, id);
    }

    //使用HQL查询
    public List<Topic> findTopicByName(String topicName){
        return (List<Topic>) getHibernateTemplate().find("from Topic t where t.name like ?", topicName+"%");
    }

    public long getTopicNum(){
        Object o = getHibernateTemplate().iterate("select count(t.id) from Topic t").next();
        return (Long) o;
    }

}
